package com.one.consumer.reserve.ui.binder;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.utils.MyToast;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.one.common.view.widget.InputLayout;
import com.one.consumer.R;
import com.one.common.common.order.extra.CustomExtra;
import com.one.consumer.reserve.model.ReserveModel;
import com.one.consumer.reserve.model.item.CustomItem;
import com.one.consumer.reserve.model.item.ReserveItem;

/**
 * Author: zhulunjun
 * Time  : 2018/10/26 9:51
 * email : zhulunjun@qq.com
 * Describe : 装修预约，设计预约
 **/
public class CustomBinder extends BaseItemBinder<CustomItem> {
    public CustomBinder(RefreshListener refreshListener) {
        super(R.layout.item_reserve);
        this.refreshListener = refreshListener;
    }

    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, @NonNull CustomItem item) {
        InputLayout ilTime = holder.getView(R.id.il_time);
        InputLayout ilType = holder.getView(R.id.il_type);
        InputLayout ilContact = holder.getView(R.id.il_contact);
        InputLayout ilPhone = holder.getView(R.id.il_phone);
        InputLayout ilAddress = holder.getView(R.id.il_address);
        ilType.setName("项目名称");
        ilTime.setText(item.getCreate_time());
        ilType.setText(item.getCustomized_name());
        ilContact.setText(item.getContact_name());
        ilPhone.setText(item.getContact_mobile());
        ilAddress.setText(item.getAddress_detail());

        holder.getView(R.id.tv_btn_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RouterManager.getInstance().go(RouterPath.CUSTOM_DETAIL,
                        new CustomExtra(item.getOrder_id()+""));
            }
        });
        TextView tvCancel = holder.getView(R.id.tv_btn_cancel);
        TextView tvDelete = holder.getView(R.id.tv_btn_delete);
        if(item.getOrder_state().equals("0")){ // 已取消
            tvCancel.setVisibility(View.GONE);
            tvDelete.setVisibility(View.VISIBLE);
        } else {
            tvCancel.setVisibility(View.VISIBLE);
            tvDelete.setVisibility(View.GONE);
        }

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setMessage("确定取消订单？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                cancel(item);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        }).show();

            }
        });

        tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setMessage("确定删除订单？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                delete(item);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        }).show();
            }
        });
    }


    private void cancel(CustomItem item){
        new ReserveModel((BaseActivity) mContext).cancelCustom(item.getOrder_id()+"", new ObserverOnResultListener<Object>() {
            @Override
            public void onResult(Object o) {
                Toaster.showLongToast("取消成功", MyToast.SUCCESS);
                refreshListener.onRefresh();
            }
        });
    }

    private void delete(CustomItem item){
        new ReserveModel((BaseActivity) mContext).deleteCustom(item.getOrder_id()+"", new ObserverOnResultListener<Object>() {
            @Override
            public void onResult(Object o) {
                Toaster.showLongToast("删除成功", MyToast.SUCCESS);
                refreshListener.onRefresh();
            }
        });
    }

    private RefreshListener refreshListener;
    public interface RefreshListener{
        void onRefresh();
    }
}
